﻿--[[
   @brief    feedback_result_frame_control
   @author   perpu
   @version  1.0
   @date     2017-03-21
--]]

feedback_result_frame_control = 
{
    init = function ()
        base_utility.log("[feedback_result_frame_control] init");
        ierd_tgp.tpf_ui.Require("feedback_result_frame\\view.lua");
        ierd_tgp.tpf_ui.Require("feedback_result_frame\\model.lua");
        -- 注册提供上传消息的服务
        comm_center.reg_service("i_service_upload_msg", feedback_result_frame_control.upload_msg);
        comm_center.listen_broadcast("i_broadcast_msg_box_new_msg_com", feedback_result_frame_control.on_broadcast_new_msg);
        comm_center.listen_broadcast("i_broadcast_msgbox_get_reddot", feedback_result_frame_control.handle_get_reddot);
    end;

    result_show = function (data_in)
        local data_out = data_in;
        feedback_result_frame_view:show(true, data_in);
        data_out.result = 0;
        return data_out;
    end;

    show_report_count_flag = function(count)
        local param = {number = count};
        event_center.send_event(group_event.SHOW_NOTIFY_COUNT_FLAG, base_utility.table_to_json(param));
        feedback_result_frame_model.unread_msg_count = count;
        if count > 0 then
            event_center.send_event(group_event.SHOW_MSG_BOX_NOTIFY_REDDOT, "");
        end
    end;

    clear_red_dot = function ()
        base_utility.log("[feedback_result_frame_control][clear_red_dot]");
        -- 调用服务清掉后台的未读消息数
        local data_in = {};
        data_in["tgp_id"] = base_utility.get_tgp_id();
        comm_center.call_service("i_service_msgbox_clear_reddot", data_in);
    end;

    on_js_callback = function(uilib, func_name, json_str, control_id)
        base_utility.log("[feedback_result_frame_control]on_js_callback, func_name:"..func_name..", json_str:"..json_str);

        local social_network =
            base_utility.get_component_adapter("ISocial_network",
                "ierd_tgp.social_network.Get_social_network_adapter();")

        if func_name == "close" then
            -- 关闭消息盒子
            -- feedback_result_frame_view:close_by_js(control_id)
        elseif func_name == "user_action_req" then
            -- 消息盒子用户操作响应
            comm_center.send_broadcast("i_broadcast_msg_box_user_action",
                base_utility.json_to_table(json_str));
        elseif func_name == "get_cookie" then
            feedback_result_frame_control.handle_get_cookie(json_str);
        elseif func_name == "clear_red_dot" then
            feedback_result_frame_control.handle_clear_red_dot();
        end
    end;

    -- 返回选择的图片数据：base64，上传到服务器的原图及缩略图的url和md5
    handle_open_img_browse_floder = function(event_id, json_str)
        base_utility.log(string.format("[feedback_result_frame_control]handle_open_img_browse_floder, json_str = %s", json_str));

        local result_table = base_utility.json_to_table(json_str);
        -- local param_table = {};
        -- param_table["retCode"] = result_table.result;
        -- param_table["data"] = result_table

        web_utility.lua_call_js("open_img_browse_floder", base_utility.table_to_json(result_table), CID_WEB_CTRL_FOR_FEEDBACK);
    end;

    -- 获取cookie接口
    handle_get_cookie = function (json_str)
        base_utility.log(string.format("[feedback_result_frame_control][handle_get_cookie]json:%s", json_str));
        local param = base_utility.json_to_table(json_str);
        if nil ~= param and nil ~= param.data then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            rsp_table.data = feedback_result_frame_model.cookie_info;   
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_ex(param.callback_func, rsp_json, CID_WEB_CTRL_FOR_FEEDBACK);
            end
        end
    end;

    -- 清空红点
    handle_clear_red_dot = function ()
        base_utility.log("[feedback_result_frame_control][handle_clear_red_dot]");
        feedback_result_frame_control.show_report_count_flag(0);
        event_center.send_event(group_event.CLEAR_MSG_BOX_NOTIFY_REDDOT, "");
        -- 调用服务清掉后台的未读消息数
        feedback_result_frame_control.clear_red_dot();
    end;

    -- 通知web收到消息推送
    send_report_to_web = function (report)
        base_utility.log(
            string.format("[feedback_result_frame_control][send_report_to_web]type:%d", report["type"]));

        local callback_func = web_register_center.get_func_by_browser(
            "recv_msg_report", CID_WEB_CTRL_FOR_FEEDBACK);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_data_table = 
                web_utility.notify_event_comm_data("recv_msg_report");
            rsp_data_table.data.data = report;
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_WEB_CTRL_FOR_FEEDBACK);
        end
    end;

    -- 通知web打开消息盒子
    notify_show_msg_box = function ()
        base_utility.log(
            string.format("[feedback_result_frame_control][notify_show_msg_box]"));

        local callback_func = web_register_center.get_func_by_browser(
            "show_msg_box", CID_WEB_CTRL_FOR_FEEDBACK);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_data_table = 
                web_utility.notify_event_comm_data("show_msg_box");
            rsp_data_table.data.data = {};
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_WEB_CTRL_FOR_FEEDBACK);
        end
    end;

    on_tgp_st_renewed = function(event_id, json_str)
        base_utility.log("[feedback_result_frame_control] on_tgp_st_renewed" .. json_str);
        local param_tbl = base_utility.json_to_table(json_str);
        if nil == param_tbl then
            base_utility.log("[feedback_result_frame_control][on_tgp_st_renewed] param_tbl is nil");
            return;
        end
        feedback_result_frame_model.cookie_info["st_key"] = param_tbl["st_key"];
        feedback_result_frame_model.cookie_info["st"] = param_tbl["st"];
        feedback_result_frame_model.cookie_info["uin"] = param_tbl["uin"];
        feedback_result_frame_model.cookie_info["tgp_id"] = param_tbl["tgp_id"];
        feedback_result_frame_model.cookie_info["tgp_ticket"] = param_tbl["web_token"];
        feedback_result_frame_model.cookie_info["tgp_biz_ticket"] = param_tbl["biz_ticket"];

        local callback_func = web_register_center.get_func_by_browser(
            "set_social_cookie", CID_WEB_CTRL_FOR_FEEDBACK);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_data_table = 
                web_utility.notify_event_comm_data("set_social_cookie");
            rsp_data_table.data.data = feedback_result_frame_model.cookie_info;
            web_utility.lua_call_js_ex(callback_func, 
                base_utility.table_to_json(rsp_data_table), CID_WEB_CTRL_FOR_FEEDBACK);
        end
    end;

    -- 收到服务器通知
    on_broadcast_new_msg = function (data_in)
        --feedback_result_frame_model.MSG_MSGBOX_SYSTEM;
        --feedback_result_frame_model.MSG_MSGBOX_PERSON;
        local one_report = {};
        one_report["type"] = data_in.msg_type;
        feedback_result_frame_control.send_report_to_web(one_report);
        feedback_result_frame_model.unread_msg_count = feedback_result_frame_model.unread_msg_count + 1;
        feedback_result_frame_control.show_report_count_flag(feedback_result_frame_model.unread_msg_count);
    end;

    -- 上传消息服务实现
    upload_msg = function (input)
        base_utility.log(string.format(
            "[feedback_result_frame][upload_msg]input:%s",
             base_utility.table_to_json(input)));

        if nil == input or nil == input.msg_type or nil == input.report then
            base_utility.log(
                "[feedback_result_frame_control][upload_msg]params is nil"
                , true);
            return;
        end

        local one_report = input.report;

        local info = comm_center.call_service("i_service_social_get_my_info", {});

        -- 上传服务器逻辑
        -- 构造消息体
        local msg_tb = {};
        msg_tb["appid"] = GAME_ID.GAME_ID_SOCIAL; -- 统一分配
        msg_tb["msgtype"] = input.msg_type;
        msg_tb["msgsubtype"] = one_report["type"];
        msg_tb["senderid"] = one_report["sender"];
        msg_tb["receiverid_list"] = info.id;
        msg_tb["jsonmsg"] = base_utility.table_to_json(one_report);
        msg_tb["timestamp"] = one_report["time"];
        local upload_msg_url = feedback_result_frame_model.upload_msg_url;
        base_utility.curl_request_async(upload_msg_url,
            "feedback_result_frame_control.on_upload_msg_rsp", false, nil, true, nil,
             base_utility.table_to_json(msg_tb));
    end;

    -- 上传消息结果
    on_upload_msg_rsp = function (result)
        if nil == result then
            base_utility.log(
                "[feedback_result_frame_control][on_upload_msg_rsp] result is null");
            return;
        end
        local param = base_utility.json_to_table(result);
        if param ~= nil and type(param) == "table" then
            if 0 == param.ErrorCode and 
                nil ~= param.ErrorInfo then
                    base_utility.log(
                        string.format(
                        "[feedback_result_frame_control][on_upload_msg_rsp]errorCode:%d, errMsg:%s",
                        tonumber(param.ErrorCode), tostring(param.ErrorInfo)), true);
            end
        end
    end;

    -- 获取未读消息数
    handle_get_unread_msg_count = function (input)
        base_utility.log(
            string.format("[feedback_result_frame][handle_get_unread_msg_count]unread_msg_count:%d",
                feedback_result_frame_model.unread_msg_count));
        local out = {count = feedback_result_frame_model.unread_msg_count};
        return out;
    end;

    handle_get_reddot = function (data)
        if nil == data then
            base_utility.log("[feedback_result_frame_control][handle_get_reddot]data is nil");
            return;
        end
        base_utility.log(string.format("[feedback_result_frame_control][handle_get_reddot]count:%d", data.count));
        feedback_result_frame_model.unread_msg_count = data.count;
        feedback_result_frame_control.show_report_count_flag(feedback_result_frame_model.unread_msg_count);
    end;
}

feedback_result_frame_control.init();